import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_05/action/states/data_manager.dart';
import 'package:flutter_app_05/widget/sdk_extends.dart';
import 'package:shared_preferences/shared_preferences.dart';


  class LoginPage extends StatefulWidget{
  @override
  _LoginPageInState createState() => _LoginPageInState();

  }

  class _LoginPageInState extends State<LoginPage>{
  TextEditingController nameController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  bool invalidName = false;
  bool invalidPassword = false;

  @override
  Widget build(BuildContext context) {
  return Scaffold(
  // appBar: AppBar(
  //   title: Text('Login'),
  // ),
  body: Padding(
  padding: EdgeInsets.all(10),
  child: ListView(
  children: <Widget>[
    GestureDetector(
      child: Container(
        width: 50.0,height: 50.0,
        padding: EdgeInsets.all(5.0),
        alignment: Alignment.bottomLeft,
        child: "images/close.png".getImage(width: 20,height: 20,fit: BoxFit.scaleDown),

      ),
      onTap: (){
        Navigator.pop(context);
      },
    ),
    SizedBox(height: 10.0,),
  Container(
  alignment: Alignment.topLeft,
  padding: EdgeInsets.fromLTRB(8,0,0,0),
  child: Text(
  '密码登录',
  style: TextStyle(color: Colors.black,fontWeight:FontWeight.w500,fontSize: 30),
  )),
  SizedBox(height: 10.0,),
  Container(
  alignment: Alignment.topLeft,
  padding: EdgeInsets.fromLTRB(8,0,0,0),
  child: Text(
  '通用供热联盟论坛账号',
  style: TextStyle(color: Colors.black12,fontWeight:FontWeight.w500,fontSize:15),
  )),
  Container(
  padding: EdgeInsets.all(10),
  child: TextField(
  controller: nameController,
  decoration: InputDecoration(
    enabledBorder:  UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.green),
    ),
  // border: OutlineInputBorder(),
  // labelText: '账号',
  hintText: '账号',
  hintStyle: TextStyle(color: Colors.green),
  // labelStyle: TextStyle(color: Colors.green),
  errorText: invalidName ? '不能为空' : null,
  ),
  )),
  Container(
  padding: EdgeInsets.fromLTRB(10,10,10,0),
  child: TextField(
  obscureText: true,
  controller: passwordController,
  decoration: InputDecoration(
  enabledBorder:  UnderlineInputBorder(
  borderSide: BorderSide(color: Colors.green),
  ),
  // border: OutlineInputBorder(),
  // labelText: '密码',
  // labelStyle: TextStyle(color: Colors.green),
  hintText: '密码',
  hintStyle: TextStyle(color: Colors.green),
  errorText: invalidPassword ? '不能为空' : null,
  ),
  )),


  // Container(
  //
  //   child: Row(
  //     children: <Widget>[
  //       FlatButton(
  //         onPressed: (){
  //
  //         },
  //         padding: EdgeInsets.fromLTRB(0, 30, 0, 30),
  //         child: Text('忘记密码'),
  //       ),
  //     ],
  //     mainAxisAlignment:MainAxisAlignment.center,
  //   ),
  // ),




  Container(
  height: 80,
  padding: EdgeInsets.fromLTRB(10, 30, 10, 0),
  child: RaisedButton(
  shape: RoundedRectangleBorder(
  borderRadius: BorderRadius.all(Radius.circular(12)),
  ),
  textColor: Colors.white,
  color: Colors.redAccent,
  child: Text('登录'),
  onPressed: (){
  print(nameController.text);
  print(passwordController.text);
  if(nameController.text == null || nameController.text.isEmpty){
  setState(() {
  invalidName = true;
  });
  }else{
  setState(() {
  invalidName = false;
  });
  }
  if(passwordController.text == null || passwordController.text.isEmpty){
  setState(() {
  invalidPassword = true;
  });
  }else{
  setState(() {
  invalidPassword = false;
  });
  }
  if(!invalidName && !invalidPassword){
    _initInfo();
  }

  },
  )
  ),
  Container(
  padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
  child: Row(
  children: <Widget>[
  // Text('没有账号?'),
  FlatButton(
  textColor: Colors.black,
  child: Text(
  '快速注册',
  style: TextStyle(fontSize: 15),
  ),
  onPressed: (){

  },
  )
  ],
  mainAxisAlignment:MainAxisAlignment.end,
  ),

  ),


  Container(
      padding: EdgeInsets.fromLTRB(0, 50, 0, 0),
      alignment: Alignment.center,
      child: Text(
      '其他账号登录',
      style: TextStyle(fontSize: 15),
      ),

  ),

  Container(
    padding: EdgeInsets.fromLTRB(0, 20, 0, 0),

    child: IconButton(
      icon: Image.asset("images/weixin.png"),
      iconSize: 80,
      onPressed: () {},

  ),
  ),




  ],
  ),
  ),
  );
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }
  Future<bool> _initInfo() async {
    var a=await DataManager.login(p: {"mac":"08:00:27:89:51:a4","pass":nameController.text,"userCode":passwordController.text}).then((value) async{


      if(value.code==0){
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setInt("iflogin", 1);
        Navigator.pop(context);
      }

      setState(() {

      });
    });
  }

  }
